#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Library.Entity;
using System.Collections;
using Library.Services;

#endregion

namespace Library
{
    public partial class FormBookBorrowAddBook : Form, IFindBook
    {
        IFormBookBorrowAddBook m_mainForm = null;
        Book m_book = null;
        BookStock m_bookStock = null;
        BookService m_BookService = new BookService();
        BookStockService m_BookStockService = new BookStockService();
        public FormBookBorrowAddBook(IFormBookBorrowAddBook frm)
        {
            InitializeComponent();
            m_mainForm = frm;
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (m_book == null)
                MessageBox.Show("No Book to add!");
            else
            {
                if (m_bookStock.BookBalance < numericUpQuantity.Value)
                {
                    MessageBox.Show("No Book stock available!, please adjust the book stock");
                    return;
                }
                m_book.Tag = numericUpQuantity.Value;
                if (m_mainForm.CheckExistBook(m_book))
                    MessageBox.Show("Book already added!");
                else
                {
                    m_mainForm.AddBook(m_book);
                    Close();
                }
            }
        }

        private void buttonFindBook_Click(object sender, EventArgs e)
        {
            FormFindBook frm = new FormFindBook(this, textBoxBookCode.Text);
            frm.ShowDialog();
        }

        public void SetBook(Book book)
        {
            m_bookStock = m_BookStockService.GetBookStockByBookId(book.Id);
            textBoxBookCode.Text = book.Code;
            textBoxBookTitle.Text = book.Name;
            textBoxAvailableQty.Text = m_bookStock.BookBalance.ToString();
            m_book = book;
        }

        private void textBoxBookCode_EnterKeyDown(object objSender, KeyEventArgs objArgs)
        {
            IList books = m_BookService.GetBookByLike(textBoxBookCode.Text);
            if (books.Count == 1)
                SetBook(books[0] as Book);
            else
                buttonFindBook.PerformClick();
        }
    }
}